package ifosup.marie.calculatrice;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Fenetre extends JFrame
{
	private static final long serialVersionUID = 1L;
	private JPanel panelGlobal = new JPanel(new BorderLayout());
	private JButton bouton1 = new JButton("1");
	private JButton bouton2 = new JButton("2");
	private JButton bouton3 = new JButton("3");
	private JButton bouton4 = new JButton("4");
	private JButton bouton5 = new JButton("5");
	private JButton bouton6 = new JButton("6");
	private JButton bouton7 = new JButton("7");
	private JButton bouton8 = new JButton("8");
	private JButton bouton9 = new JButton("9");
	private JButton bouton0 = new JButton("0");
	private JButton boutonPoint = new JButton(".");
	private JButton boutonEgal = new JButton("=");
	private JButton boutonPlus = new JButton("+");
	private JButton boutonMoins = new JButton("-");
	private JButton boutonMultiplie = new JButton("*");
	private JButton boutonDivise = new JButton("/");
	private JButton boutonC = new JButton("C");
	private JLabel label = new JLabel("0");
	private int operateur;
	private double operande;
	
	public Fenetre()
	{
		this.setTitle("Calculatrice");
		this.setSize(300,400);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(panelGlobal);
				
		GridBagLayout gbl = new GridBagLayout();
		panelGlobal.setLayout(gbl);
		
		label.setPreferredSize(new Dimension(280, 70));
		label.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		GridBagConstraints contrainteLabel = new GridBagConstraints(); 
		contrainteLabel.fill = GridBagConstraints.BOTH;
		contrainteLabel.gridx = 0;
		contrainteLabel.gridy = 0;
		//contrainteLabel.gridheight = 2;
		contrainteLabel.gridwidth = 2;
		panelGlobal.add(label,contrainteLabel);
		
		JPanel panelChiffre = new JPanel();
		JPanel panelOperation = new JPanel ();
		
		GridBagConstraints contrainteChiffre = new GridBagConstraints(); 
		contrainteChiffre.fill = GridBagConstraints.BOTH;
		contrainteChiffre.gridx = 0;
		contrainteChiffre.gridy = 1;
		//contrainteChiffre.gridheight = 5;
		//contrainteChiffre.gridwidth = 3;
		panelGlobal.add(panelChiffre,contrainteChiffre);

		GridBagConstraints contrainteOperation = new GridBagConstraints();
		contrainteOperation.fill = GridBagConstraints.BOTH;
		contrainteOperation.gridx = 1;
		contrainteOperation.gridy = 1;
		//contrainteOperation.gridheight = 5;
		//contrainteOperation.gridwidth = 2;
		panelGlobal.add(panelOperation,contrainteOperation);
		
		panelChiffre.setPreferredSize(new Dimension(210, 290));
		panelChiffre.setLayout(new GridLayout(4,3,2,2));
		panelChiffre.add(bouton1);
				
		bouton1.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherChiffre(1, label);
			}
		});
		panelChiffre.add(bouton2);
		bouton2.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherChiffre(2, label);
			}
		});
		panelChiffre.add(bouton3);
		bouton3.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherChiffre(3, label);
			}
		});
		panelChiffre.add(bouton4);
		bouton4.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherChiffre(4, label);
			}
		});
		panelChiffre.add(bouton5);
		bouton5.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherChiffre(5, label);
			}
		});
		panelChiffre.add(bouton6);
		bouton6.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherChiffre(6, label);
			}
		});
		panelChiffre.add(bouton7);
		bouton7.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherChiffre(7, label);
			}
		});
		panelChiffre.add(bouton8);
		bouton8.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherChiffre(8, label);
			}
		});
		panelChiffre.add(bouton9);
		bouton9.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherChiffre(9, label);
			}
		});
		panelChiffre.add(boutonPoint);
		boutonPoint.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				Operation.afficherPoint(label);
			}
		});
		
		panelChiffre.add(bouton0);
		bouton0.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherChiffre(0, label);
			}
		});
		panelChiffre.add(boutonEgal);
		boutonEgal.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				Operation.afficherResultat(operateur, operande, Double.parseDouble(label.getText()), label);
			}
		});
		
		panelOperation.setPreferredSize(new Dimension(70, 290));
		panelOperation.setLayout(new GridLayout(5,1,1,1));
		
		panelOperation.add(boutonPlus);
		boutonPlus.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				operande = Double.parseDouble(label.getText());
				operateur = 1;
				label.setText(" ");
			}
		});
		
		panelOperation.add(boutonMoins);
		boutonMoins.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				if ((label.getText().equals(" ")) || (label.getText().equals("0")))
				{
					label.setText("-");
				}
				else
				{
				operande = Double.parseDouble(label.getText());
				operateur = 2;
				label.setText(" ");
				}
			}
		});
		
		panelOperation.add(boutonMultiplie);
		boutonMultiplie.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				operande = Double.parseDouble(label.getText());
				operateur = 3;
				label.setText(" ");
			}
		});
		
		panelOperation.add(boutonDivise);
		boutonDivise.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				operande = Double.parseDouble(label.getText());
				operateur = 4;
				label.setText(" ");
			}
		});
		
		panelOperation.add(boutonC);
		boutonC.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				label.setText(" ");
				operande = 0;
				operateur = 0;
			}
		});
		

		
		//this.pack();
		this.setVisible(true);
	}
}
